package it.davial.mgwt.server;

import it.davial.mgwt.client.Area;
import it.davial.mgwt.client.Patient;
import it.davial.mgwt.client.Visit;
import it.davial.mgwt.client.service.DbService;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class DbServiceImpl extends RemoteServiceServlet implements DbService{

	private static final Logger logger = Logger.getLogger("DbServiceImpl");
	/**
	 * 
	 */
	private static final long serialVersionUID = 834283869231952266L;

	@Override
	public String greetServer(String name) throws IllegalArgumentException {
		return "OK";
	}

	@Override
	public List<Patient> getAllPatient() {
		logger.info("getting all patients");
		List<Patient> result = null;
		
		try {
			result = PatientUtils.findAllPatient();
		} catch (Exception e) {
			logger.info("error getting all patiengs, error=" + e);
			e.printStackTrace();
		}
		
		return result;
	}

	@Override
	public boolean savePatient(Patient patient) throws Exception {
		logger.info("saving patient..");
		Integer id = patient.getIdPatient();
		logger.info("id=" + id);
		if ( id != null && id > 0)
			return PatientUtils.mergePatient(patient);
		else 
			return PatientUtils.createPatient(patient);
		
	}

	@Override
	public boolean deletePatient(Patient patient) throws Exception {
		boolean bret = false;
		try{
			bret = PatientUtils.deletePatient(patient);
		}catch ( Exception e ){
			logger.info("error deleting patient," + e);
			e.printStackTrace();
		}
		return bret;
	}

	@Override
	public List<Patient> getFilteredPatient(String surname, String date) {
		logger.info("getting getFilteredPatient");
		List<Patient> result = null;
		
		try {
			result = PatientUtils.findPatientBySurnameAndDate(surname, date);
		} catch (Exception e) {
			logger.info("error getting getFilteredPatient, error=" + e);
			e.printStackTrace();
		}
		
		return result;
	}

	@Override
	public List<Area> findAreaById(int idFigure) {
		logger.info("getting findAreaById");
		List<Area> resultList = new ArrayList<Area>(1);
		Area result = null;
		
		try {
			result = AreaUtils.findAreaById(idFigure);
			resultList.add(result);
		} catch (Exception e) {
			logger.info("error getting findAreaById, error=" + e);
			e.printStackTrace();
		}
		
		return resultList;
	}

	@Override
	public List<Visit> getAllVisits() {
		logger.info("getting getAllVisits");
		List<Visit> result = null;
		
		try {
			result = VisitUtils.findAllVisit();
		} catch (Exception e) {
			logger.info("error getAllVisits, error=" + e);
			e.printStackTrace();
		}
		
		return result;
	}

	@Override
	public List<Visit> getFilteredVisits(String patientId, String date) {
		logger.info("getting getFilteredVisits");
		List<Visit> result = null;
		
		try {
			
			result = VisitUtils.findVisitByIdPersonAndDate(patientId, date);
		} catch (Exception e) {
			logger.info("error getFilteredVisits, error=" + e);
			e.printStackTrace();
		}
		
		return result;
	}

	@Override
	public boolean saveVisit(Visit visit) throws Exception {
		logger.info("saving visit..");
		Integer id = visit.getIdVisit();
		logger.info("id=" + id);
		if ( id != null && id > 0)
			return VisitUtils.mergeVisit(visit);
		else 
			return VisitUtils.createVisit(visit);
	}

	@Override
	public boolean deleteVisit(Visit visit) throws Exception {
		boolean bret = false;
		try{
			bret = VisitUtils.deleteVisit(visit);
		}catch ( Exception e ){
			logger.info("error deleting visit," + e);
			e.printStackTrace();
		}
		return bret;
	}

}
